package com.ants.logic.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ants.logic.app.dao.TblOrderMapper;
import com.ants.logic.app.dto.DecreaseStorageDto;
import com.ants.logic.app.dto.OrderCreateDto;
import com.ants.logic.app.entity.TblOrder;
import com.ants.logic.app.service.TblOrderService;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 蚂蚁会花呗
 * @date 2021/9/9 15:20
 */
@Service
public class TblOrderServiceImpl extends ServiceImpl<TblOrderMapper, TblOrder> implements TblOrderService {

    static Log log = LogFactory.getLog(TblOrderServiceImpl.class);

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void create(OrderCreateDto createDto) {
        log.info("---------->开始交易");
        //创建订单
        log.info("创建订单开始！");
        this.save(createDto);
        log.info("创建订单结束！");

        /**
         * 消息生产者
         * 扣减库存交给消息中间件，发送异步消息
         */
        //扣减库存
        log.info("扣减库存开始");
        rocketMQTemplate.syncSend("my-topic", JSON.toJSONString(new DecreaseStorageDto(createDto.getProductId(),createDto.getCount())));
        log.info("扣减库存结束");

        log.info("交易结束！");
    }
}
